/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package threads;
import java.util.*;
import java.util.concurrent.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Bertil
 */
public class Ex125 {
    public Ex125(){
        try {
            runner();
        } catch (InterruptedException ex) {
            Logger.getLogger(Ex125.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public static void runner() throws InterruptedException {
        BlockingQueue<String> buffer = new LinkedBlockingQueue<String>(); // En typ av lista som automatiskt sköter notifications när operationer utförts

        Scanner sc = new Scanner(System.in);
        System.out.println("Antal Prod125er? ");
            System.out.flush();
            Prod125[] p = new Prod125[sc.nextInt()];

        for(int i=0; i<p.length; i++){
            System.out.println("Prod125 nr " + (i+1) + ":");
            System.out.print(" Tidsintervall? ");
            System.out.flush();
            int time = sc.nextInt();
            System.out.print(" Text? ");
            System.out.flush();
            String txt = sc.next();
            p[i] = new Prod125(txt, time, buffer); // lägger till ny producent
        }

        System.out.print(" Antal Kons125er? ");
            System.out.flush();
            Kons125[] k = new Kons125[sc.nextInt()];

        for(int i=0; i < k.length; i++){
            System.out.println("Kons125 nr " + (i+1) + ":");
            System.out.print("   Tidsintervall? ");
                System.out.flush();
            int time = sc.nextInt();
            k[i] = new Kons125(time, buffer); // lägger till ny konsument
        }

        Thread.currentThread().setPriority(Thread.MAX_PRIORITY); // ge aktiv tråd högsta prioritet
        for(int i=0; i < p.length; i++){
            p[i].activity.start(); // sätter igång trådarna
        }
        for(int i=0; i < k.length; i++){
            k[i].activity.start(); // sätter igång trådarna
        }
        Thread.sleep(60000);
        System.out.println("\nAntal kvar i kön: " + buffer.size());
        System.exit(0);
    }
}